using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;
using System.Collections.Generic;

namespace IQIGame.Onigao.GamePlay
{
    public class UIN_PetPabilityGroup : UIBaseNode
    {
        #region Template Generate,don't modify
        protected class UIB_UIN_PetPabilityGroup
        {
            #region ObjectBinding Generate 
            public UnityEngine.GameObject attribute { protected set; get; }
            public virtual void InitBinding(ObjectBinding __binding)
            {
                __binding.TryGetVariableValue<UnityEngine.GameObject>("attribute", out var __tbv0);
                this.attribute = __tbv0;
            }
            #endregion ObjectBinding Generate 
        }
        #endregion Template Generate,don't modify

        #region fields

        private ListScrollAdapter<UIN_PetPabilityAttribute> attributes;

        private Pet pet = null;
        private int groupIndex = 0;
        private IReadOnlyList<int> dataList = null;
        private System.Action<int, int> callOnClickAttribute = null;

        #endregion fields

        #region properties

        protected UIB_UIN_PetPabilityGroup ui { get; set; }
        protected UIMsgDispatcher uiMsgDispatcher => this._msgDispatcher as UIMsgDispatcher;

        #endregion properties

        protected override void BeforeInit()
        {
            ui = new UIB_UIN_PetPabilityGroup();
            ui.InitBinding(this.csObjBind);
        }

        protected override void OnInit()
        {
            this.attributes = this.InitListScroll<UIN_PetPabilityAttribute>(this.ui.attribute, this.OnAttributeChanged);
        }

        protected override void OnShow()
        {

        }

        protected override void OnHide()
        {
            this.ClearData();
        }

        /// <summary>
        /// 添加UGUI侦听
        /// </summary>
        protected override void AddUGUIListener()
        {

        }

        /// <summary>
        /// 添加全局侦听（MsgDispatcher）
        /// </summary>
        protected override void AddMsgListeners()
        {

        }

        protected override void OnDispose()
        {
            this.ClearData();
        }

        #region methonds

        public void Show(Pet pet, int groupIndex, System.Action<int, int> funcClickAttribute)
        {
            var pabilityAttributeData = pet.GetPabilityGroupList(groupIndex);
            int cnt = pabilityAttributeData.Count;
            if (cnt == 0)
            {
                this.Hide();
                return;
            }
            this.SetData(pet, groupIndex, funcClickAttribute);
            this.Show();
            this.ShowAttributes();
        }

        private void SetData(Pet pet, int groupIndex, System.Action<int, int> funcClickAttribute)
        {
            this.pet = pet;
            this.groupIndex = groupIndex;
            this.callOnClickAttribute = funcClickAttribute;
            this.dataList = pet.GetPabilityGroupList(groupIndex);
        }

        private void ClearData()
        {
            this.dataList = null;
            this.pet = null;
            this.groupIndex = 0;
            this.callOnClickAttribute = null;
        }

        private void ShowAttributes()
        {
            this.attributes.RefillCells(this.dataList.Count);
        }

        private void OnAttributeChanged(UIN_PetPabilityAttribute item, int index)
        {
            int pabilityAttributeCid = this.dataList[index];
            UIPetSlotType slotType = PetModule.Instance.GetPabilityAttributeSlotType(this.pet, pabilityAttributeCid);
            item.Show(pabilityAttributeCid, slotType, index, this.OnClickAttribute);
        }

        private void OnClickAttribute(int cellIndex)
        {
            this.ExcuteCall(cellIndex);
        }

        private void ExcuteCall(int attributeIndex)
        {
            this.callOnClickAttribute?.Invoke(this.groupIndex, attributeIndex);
        }
        #endregion methonds
    }
}
